
$(function(){
	$("#drugTable").datagrid({
		pagination:true,
		idField:"id"
	});
	$("#dictionaryParent").datagrid({
		idField:"id"
	}) 
	
	$("#dictionaryParent").datagrid({
		onClickRow:function (){
			var id = $("#dictionaryParent").datagrid('getSelected');
			var postData = {'parent.id':id.id};
			$("#dictionaryTable").datagrid("reload",postData);	
		}
		
	})
})
function unitFormatter(value,row,index){
	console.log(row);
	return row.unit.value;
}
function typeFormatter(value,row,index){
	console.log(row);
	return row.type.value;
}
//根据name条件查询
	function setCondition(){
		
		var postData = {name : $("#name").val()};
		var ids = $("#drugType").combobox("getValue");
		postData["type.id"] = ids;
		$("#drugTable").datagrid("reload",postData);
	}
	function resetCondition(){
		$("#userCondition").form("clear");
	}
	//添加药品
	function addDrug(){
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title:"添加药品",
			width:450,
			height:450,
			modal:true,
			href:"drug/drugForm",
			onClose:function(){$(this).dialog("destroy");},
			buttons:[{
				text:"确定",
				handler:function(){
					$("#drugForm").form("submit",{
						url:"drug/add",
						success:function(data){
							d.dialog("close");
							$("#drugTable").datagrid("reload");
						}
					});
				}
			},{
				text:"取消",
				handler:function(){
					d.dialog("close");
				}
			}]
		});
	}
	//修改
	function editDrug(){
		var row = $("#drugTable").datagrid("getSelected");
		if(row == null){
			return;
		}
		$("#drugTable").datagrid("clearSelections");
		$("#drugTable").datagrid("selectRecord",row.id);
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title:"编辑",
			width:450,
			height:450,
			modal:true,
			href:"drug/drugForm",
			onClose:function(){$(this).dialog("destroy");},
			onLoad:function(){
				$.post("drug/view",{id:row.id},function(data){
					
					$("#drugForm").form("load",data);
					$("#unit").combobox("select",data.unit.id);
					$("#type").combobox("select",data.type.id);
					
					
				});
			},
			buttons:[{
				text:"确定",
				handler:function(){
					$("#drugForm").form("submit",{
						url:"drug/edit",
						success:function(data){
							d.dialog("close");
							$("#drugTable").datagrid("reload");
						}
					});
				}
			},{
				text:"取消",
				handler:function(){
					d.dialog("close");
				}
			}]
		});
	}
	function deleteDrug(){
		var selRows = $("#drugTable").datagrid("getSelections");
		if(selRows.length == 0){
			$.messager.alert("提示","请选中要删除的数据行！","warning");
			return;
		}
		$.messager.confirm("提示","确定要删除选中的数据吗？",function(r){
			if(r){
				//3. 发送异步请求，带选中行的编号
				//拼接删除条件
				var postData = "";
				$.each(selRows,function(i){
					postData += "ids="+this.id;
					if(i < selRows.length - 1){
						postData += "&";
					}
				});
			}
			$.post("drug/deleteDrug",postData,function(data){
				if(data.result == true){
					//4. 删除成功后，刷新表格 reload
					$("#drugTable").datagrid("reload");
				}
			});
		});
	}